from sqlalchemy import create_engine
from sqlalchemy import Table,Column,Integer,String,MetaData,ForeignKey,Float
from sqlalchemy.orm import mapper
from sqlalchemy.orm import sessionmaker
from player import Player

#create session
engine = create_engine("sqlite+pysqlite:///database.db",echo = True)
Session = sessionmaker(bind=engine)
session = Session()

metadata = MetaData()
#name table, set columns
drazah_players = Table("players",metadata,
                       Column("ID",Integer),
                       Column("name",String,primary_key = True),
                       Column("wins",Integer),
                       Column("losses",Integer),
                       Column("draws",Integer),
                       Column("score",Float),
                       Column("achievements", Integer)
                       )


#Map class to db
mapper(Player,drazah_players)
Session.configure(bind=engine)
metadata.create_all(engine) 
session.commit()

#Print all players in database
for player in session.query(Player).order_by(Player.ID):
     print player.showStats()


# p=Player("Ted",1121,1)
# p.addLosses()
# p.addLosses()
# p.addWins()

# session.add(p)
# session.commit()

#  LocalWords:  ForeignKey sessionmaker MetaData metadata addPlayer nameP TODO
#  LocalWords:  updatePlayer newName winsNew lossesNew drawsNew newPlayer
#  LocalWords:  printPlayer Drazah addWins showStats
